package controller.web;

import business.OrderList;
import business.ObatListing;
import business.DaftarUser;
import business.Obat;
import business.User;
import business.Admin;
import business.Order;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;

public class OrderServlet extends Servlet {

    public OrderServlet(){
        super();
        setTemplate("/WEB-INF/order.jsp");
    }

    @Override
    protected void process() {

        setMessage("");
        if (getRequest().getParameter("act") != null && getRequest().getParameter("act").equals("add")) {
            try {
                if (validate_field()) {
                    HttpSession session = getRequest().getSession();
                        OrderList db = new OrderList();
                        Order m = new Order();
                        int kuantitas = Integer.parseInt(getRequest().getParameter("kuantitas"));
                            m.setUser(session.getAttribute("username"));
                            m.setObat(getRequest().getParameter("kode"));
                            m.setKuantitas(kuantitas);
                            db.tambahOrder(m);

                } else {
                    getResponse().sendRedirect("dataKosong");
                }
                getResponse().sendRedirect("userHome");
            } catch (IOException ex) {
                Logger.getLogger(ObatAddServlet.class.getName()).log(Level.SEVERE, null, ex);
            }



        }
    }


        boolean validate_field() {

        String kode = getRequest().getParameter("kode");
        String kuantitas = getRequest().getParameter("kuantitas");

        if(kode == null|kuantitas == null){
            return false;
        }

        return true;
    }


    }
